package com.xiaocz.hblsubstitutedriving.wxapi;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.xiaocz.hblsubstitutedriving.App;
import com.xiaocz.hblsubstitutedriving.R;


public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    // 目标打印标识
    protected static String TAG;
    // 上下文对象
    protected Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        // 初始化打印标识
        TAG = this.getClass().getSimpleName();
        mContext = this;
        //如果没回调onResp，八成是这句没有写
        App.mWxApi.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    // app发送消息给微信，处理返回消息的回调
    @Override
    public void onResp(BaseResp basResp) {

        if (basResp instanceof SendAuth.Resp) {
            SendAuth.Resp resp = (SendAuth.Resp) basResp;
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    //拿到了微信返回的code,立马再去请求access_token
                    String code = resp.code;
                    Log.e("微信回调", "code : " + code);
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    App.getInstance().showMessage("取消登录");
                    finish();
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:
                    App.getInstance().showMessage("登录失败");
                    finish();
                    break;
                default:
                    App.getInstance().showMessage("登录失败");
                    finish();
                    break;
            }
        } else if (basResp instanceof SendMessageToWX.Resp) {
            finish();
            SendMessageToWX.Resp resp = (SendMessageToWX.Resp) basResp;
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    App.getInstance().showMessage("分享成功");
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    App.getInstance().showMessage("取消分享");
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:
                    App.getInstance().showMessage("分享失败");
                    break;
                default:
                    App.getInstance().showMessage("分享失败");
                    break;
            }
        }
    }
}